# @Time:        2024/7/5 下午3:49
# @USER:        lihuizhuo
# @Site:        www.swust.edu.cn
# @File:        M
# @Software:    PyCharm
# @license:     Copyright(C), swust
# @Author:      huizhuo
# @Email:       huizhuoli@foxmail.com
# @Version:     V1.1.0
import enum

import pygame


class WalkDir(enum.IntEnum):
    down = 0
    left = 1
    right = 2
    up = 3


class DirWalkByAImage:
    '''
    一张图 4*4人物 下 左 右 上
    '''
    image_count = 16
    image_col = 4
    image_width = 49
    image_height = 74
    walk_dir = WalkDir.down

    def __init__(self, path_name: str, image_width: int,
                 image_height: int):
        """
        初始化
        :param path_name: 图片路径
        :param image_width: 单张图片宽度
        :param image_height: 单张图片高度
        :param is_loop: 是否循环
        """
        self.path_name = path_name
        self.image_index = 0
        self.action_images = []  # 四个方向的图片数组
        self.image_height = image_height
        self.image_width = image_width

        print(path_name)
        self.image = pygame.image.load(path_name)  # .convert()
        for i in range(0, self.image_count):
            row = int(i / self.image_col)
            col = i % self.image_col
            # print(row, col)
            if col == 0:
                dir = []
                self.action_images.append(dir)

            rect = (col * self.image_width,
                    row * self.image_height,
                    self.image_width,
                    self.image_height)
            print(rect)
            image = self.image.subsurface(rect)
            dir.append(image)

    def get_image(self) -> pygame.Surface:
        """
        获取当前显示图片
        :return:
        """
        current_image = self.action_images[self.walk_dir][self.image_index]
        self.image_index += 1
        if self.image_index >= self.image_col:
            self.image_index = 0

        return current_image

    def set_dir(self, dir: WalkDir):
        """
        设置方向
        :param dir:  方向
        :return:
        """
        self.walk_dir = dir


class WalkSoldier(pygame.sprite.Sprite):
    hp = 100
    name = '某军'
    pos_x = 100
    pos_y = 350
    image_width = 45
    image_height = 70

    walk_action = None
    rect = ()

    def __init__(self, walk_image_path: str, pos_x, pos_y, name, hp):
        super(WalkSoldier, self).__init__()
        self.walk_action = DirWalkByAImage(walk_image_path,
                                           self.image_width, self.image_height)
        self.walk_action.set_dir(WalkDir.down)
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.name = name
        self.hp = hp
        self.rect = pygame.Rect(self.pos_x - 5, self.pos_y - 10, self.image_width - 10, self.image_height - 20)

    # 0 下 1 左  2 右 3 上
    def run(self, key: int):
        if key == pygame.K_DOWN:
            self.walk_action.set_dir(WalkDir.down)
            self.pos_y += 10
        elif key == pygame.K_UP:
            self.walk_action.set_dir(WalkDir.up)
            self.pos_y += -10
        elif key == pygame.K_LEFT:
            self.walk_action.set_dir(WalkDir.left)
            self.pos_x += -10
        elif key == pygame.K_RIGHT:
            self.walk_action.set_dir(WalkDir.right)
            self.pos_x += 10

    def draw(self, surface: pygame.Surface):
        """
        绘制
        :param surface:
        :param x:
        :param y:
        :return:
        """
        current_image = self.walk_action.get_image()
        surface.blit(current_image, (self.pos_x, self.pos_y))


def step4_show_man():
    """
    step4: 显示自由运动的人
    :return:
    """
    # 初始化pygame
    pygame.init()
    # 获取游戏时钟
    clock = pygame.time.Clock()
    # 创建游戏的窗口 640 * 236 根据要显示图片的大小设置
    screen = pygame.display.set_mode((1280, 720), 0, 32)
    # 加载图片文件
    background = pygame.image.load("resources/images/welcome/welcome.jpg").convert()
    # 精灵
    man = WalkSoldier("resources/images/boy1.png", 50, 300,
                      "小明", 100)

    while True:
        # 游戏循环
        for event in pygame.event.get():
            # 关闭事件，进行退出处理
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.KEYDOWN:
                man.run(event.key)

            # elif event.type == pygame.KEYDOWN:
            #     japanese.run(event.key)
        # 绘制图片到显示窗口
        screen.blit(background, (0, 0))
        # 显示角色在（200，200）的位置
        man.draw(screen)

        # 通过时钟对象指定循环频率
        clock.tick(20)
        # 调用flip方法更新显示,也可以使用update方法
        pygame.display.flip()



if __name__ == '__main__':
    """
    通过main函数调用step4_show_janpanese
    """
    step4_show_man()
